package com.lei.view.bill;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

import com.lei.applt.DateField;
import com.lei.applt.HorizontalScrollBarUI;
import com.lei.applt.MyButton;
import com.lei.applt.VerticalScrollBarUI;
import com.lei.dao.bill.BillDao;
import com.lei.dao.bill.BusinDao;
import com.lei.dao.bill.UserDao;
import com.lei.emun.FilePathEnum;
import com.lei.modal.bill.Bill;
import com.lei.modal.bill.Busin;
import com.lei.modal.bill.User;
import com.lei.util.FrameUtil;
import com.lei.util.Function;

public class BillManager extends JPanel{

	private static final long serialVersionUID = 1L;
	private   int width = 1030,height = 800;
	//private static   final String[] columnNames = { "序号","消费用户" , "消费日期", "消费代码", "消费金额", "消费商家", "备注"};//列名最好用final修饰
	private static   final String[] columnNames = { "序号","消费用户" , "消费日期",  "消费金额", "消费商家"};//列名最好用final修饰
    private MyButton add,delete,serch,showdetil;
    private DateField startDate,endDate;
    private JLabel ldate,lfenge,result;
    
	private ImageIcon sricon,addbtnImg,delbtnImg,detailbtnImg;//查询按钮
	
    private   JScrollPane pane3 ;
    private   JTable friends ;
    private  List<Bill> bills;
    private  Hashtable<String,User> htuser;
    private  Hashtable<String,Busin> busin;
    
    private  BillDao billdao = new BillDao();
    private  UserDao userdao = new UserDao();
    private  BusinDao busindao = new BusinDao();
    private Double resultmoney;
    private BillDetail billdetail;
    private CardLayout card;//卡片
    private JPanel center,querycriteriagroup;//
    
	
	public BillManager() {
		initImage();
		initFrame();
		initAssembly();
		addListenner();
		repaint();
	}
	
	public void initImage(){
		sricon = FrameUtil.getImage(FilePathEnum.UI, "list_search.png");
		addbtnImg = FrameUtil.getImage(FilePathEnum.UI, "list_add.png");
		delbtnImg = FrameUtil.getImage(FilePathEnum.UI, "list_delete.png");
		detailbtnImg = FrameUtil.getImage(FilePathEnum.UI, "list_detail.png");
	}
	
	public void initFrame(){
    	this.setVisible(true);
    	this.setSize( width, height-100);
    	this.setLayout(null);
    	this.setBackground(Color.pink);
    	this.setOpaque(false);
    	this.repaint();
    }
	
	
	public void initAssembly(){
		startDate = new DateField();
		endDate = new DateField();
		
		ldate = new JLabel("日期:");
		lfenge = new JLabel("-");
		result = new JLabel("合计:");
		
		center = new JPanel();
		center.setOpaque(false);
		card = new CardLayout();
		querycriteriagroup = new JPanel();//查询条件的分组框
		querycriteriagroup.setLayout(null);
		querycriteriagroup.setOpaque(false);
	
		serch =new MyButton(sricon, 70, 25, "查询  ");serch.setBackground(new Color(51, 122, 183));
		add = new MyButton(addbtnImg, 70, 25, "添加  ");add.setBackground(new Color(51, 122, 183));
		delete = new MyButton(delbtnImg, 70, 25, "删除  ");delete.setBackground(new Color(51, 122, 183));
		showdetil = new MyButton(detailbtnImg, 70, 25, "详细  ");showdetil.setBackground(new Color(51, 122, 183));
		
		  
		friends = new JTable ();
		
		querycriteriagroup.setBounds(-2, 0, width+4, 65);
	
		//querycriteriagroup.setBorder(BorderFactory.createLineBorder(Color.blue));//设置面板边框颜色
		querycriteriagroup.setBorder(BorderFactory.createTitledBorder("查询条件"));
	
		ldate.setBounds(15, 15, 50, 30);
		lfenge.setBounds(165,15, 10, 30);
		result.setBounds(0, width-400, width, 50);
		
		
		startDate.setBounds(50,16, 110, 26);
		endDate.setBounds(180,16, 110, 26);
		
		startDate.setBorder(BorderFactory.createLineBorder(Color.GRAY));endDate.setBorder(BorderFactory.createLineBorder(Color.GRAY));
	
		
		serch.setBounds(width-70-5,70, 70, 25);
		add.setBounds(0, 70, 70, 25);
		delete.setBounds(80,70, 70, 25);
		showdetil.setBounds(155, 70, 70, 25);
		
	
		pane3 = new JScrollPane ();
		pane3.setBounds(0,100,width, width-500);
		pane3.getVerticalScrollBar().setUI(new VerticalScrollBarUI());
		pane3.setOpaque(false);
		pane3.getViewport().setOpaque(false);
		
	
		this.add(ldate);this.add(lfenge);this.add(result);
		
		this.add(serch);this.add(add);this.add(delete);	this.add(showdetil);
		querycriteriagroup.add(startDate);querycriteriagroup.add(endDate);
		
		
		center.setBounds(0, 100, width, width-500);
		
		this.add(querycriteriagroup);
		this.add(center);center.setLayout(card);
		
		
		center.add(pane3,"001");
	}
	
	public void addListenner(){
    	serch.getButton().addActionListener(new MyListenner());
        add.getButton().addActionListener(new MyListenner());
        delete.getButton().addActionListener(new MyListenner());
        showdetil.getButton().addActionListener(new MyListenner());
	}
	
	//添加卡片并显示
	public void addCard(int id){
		billdetail = new BillDetail(id);
		JScrollPane jsp = new JScrollPane(billdetail);
		jsp.setSize(new Dimension(width,width-500));
		jsp.getVerticalScrollBar().setUI(new VerticalScrollBarUI());
		jsp.getHorizontalScrollBar().setUI(new HorizontalScrollBarUI());
		jsp.getViewport().setOpaque(false);
		jsp.setOpaque(false);
		billdetail.setPreferredSize(jsp.getViewport().getPreferredSize());
		center.add(jsp,"002");
	}
	
	//显示卡片
	public void showCard(String cardName){
		card.show(center, cardName);
	}
	
	  /**刷新表格的方法
     * 
     */
    public void flashTable( DefaultTableModel model ){
    	 
		  model.fireTableDataChanged();
		  friends.setModel(model);
		  friends.setPreferredScrollableViewportSize(new Dimension(600, 500));//设置表格的大小
		  friends.setRowHeight (30);//设置每行的高度为20
		  //friends.setRowHeight (0, 20);//设置第1行的高度为15
		  friends.setRowMargin (5);//设置相邻两行单元格的距离
		  friends.setRowSelectionAllowed (true);//设置可否被选择.默认为false
/*		  friends.setSelectionBackground (Color.white);//设置所选择行的背景色
		  friends.setSelectionForeground (Color.red);//设置所选择行的前景色
		  friends.setGridColor (Color.black);//设置网格线的颜色*/
		  friends.selectAll ();//选择所有行
		  //friends.setRowSelectionInterval (0,2);//设置初始的选择行,这里是1到3行都处于选择状态
		  friends.clearSelection ();//取消选择
		  friends.setDragEnabled (false);//不懂这个
		  friends.setShowGrid (true);//是否显示网格线
		  friends.setShowHorizontalLines (true);//是否显示水平的网格线
		  friends.setShowVerticalLines (true);//是否显示垂直的网格线
		  //friends.setValueAt ("tt", 0, 0);//设置某个单元格的值,这个值是一个对象
		  friends.doLayout ();
		  friends.setBorder(null);
		  //friends.setBackground (Color.lightGray);

		  //设置表格表头颜色和斑马线
		  FrameUtil.setTableHeaderAndBanMa(friends);
		  
		  pane3.setViewportView(friends);
		  pane3.getViewport().setOpaque(false);
		  pane3.setOpaque(false);
		  
		  
		  
/*			if(bills.size() > 0){
				values = new JChartLibSerie("柱状图");
		   
	  		for(int i = 0;i<bills.size();i++){
	  			values.addValue(bills.get(i).getCmoney());
	  		}
		    
		    dataset =  new JChartLibDataSet();
		    dataset.addDataSerie(values);
		    
		    chart = new JChartLibAreachart(
		    		"标题", "X轴", "Y轴", dataset);
		    chartPanel = new JChartLibPanel(chart);
		    chartPanel.setBounds(0,400, width, 300);
		    this.add(chartPanel);
			}*/
	    
	    this.repaint();
     }
    
    public DefaultTableModel getModeal(List<Bill> bills){
    	resultmoney = 0.0;
		htuser = userdao.getAllUserToKV();
		busin  = busindao.getAllBusinKV();
    	String[][] datas = new String[bills.size()][5];
        for(int i =0;i<bills.size();i++){
        	resultmoney += bills.get(i).getCmoney();
        	datas[i][0] = (i+1)+"";
        	datas[i][1] = htuser.get(bills.get(i).getConsumptionuser() + "").getUsername();
        	datas[i][2] = Function.DateFrameToStr(bills.get(i).getConsumptiondate()) + "";
        	datas[i][3] = Function.doubleTransform(bills.get(i).getCmoney());
            datas[i][4] = busin.get(bills.get(i).getCtobusin() + "").getBusname();
        	
        	
        }
        DefaultTableModel model = new DefaultTableModel();
        model.setDataVector(datas, columnNames);
        
        return model;
    }
    
    
    class MyListenner implements ActionListener{
    	
    	public MyListenner() {
		}
    	
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(serch.getButton())){
				showCard("001");
				//获取选择的日期
            
				
		         String startday = "";
		         String endday = "";
				try {
					startday = Function.changeDateFormat(Function.DateFrameToStr(startDate.getDate()),"yyyy-MM-dd");
					endday = Function.changeDateFormat(Function.DateFrameToStr(endDate.getDate()),"yyyy-MM-dd");
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
		      
		         
				bills = billdao.queryAllBill(startday,endday);
				 DefaultTableModel model = getModeal(bills);
    		   //刷新表格的方法
                 flashTable(model);
		         result.setText("合计:" + Function.doubleTransform(resultmoney));

			}
			if(e.getSource().equals(add.getButton())){
				new BillAdd();
			}
			
			if(e.getSource().equals(delete.getButton())){
				//获取选中 的表格下标
				int index = friends.getSelectedRow();
				if(index == -1){
					JOptionPane.showMessageDialog(null, "你还未选中数据");
				}else{
					int choose = JOptionPane.showConfirmDialog(null, "确认删除吗？");
					if(choose == 0){
					    Bill bill = bills.get(index);
						boolean isdel = billdao.deleteRow(bill);
						if(isdel){
							JOptionPane.showMessageDialog(null, "删除成功");
							bills = billdao.queryAllBill();
							DefaultTableModel model = getModeal(bills);
			    		   //刷新表格的方法
			                 flashTable(model);
					         result.setText("合计:" + Function.doubleTransform(resultmoney));
						}
						else if(!isdel)JOptionPane.showMessageDialog(null, "删除失败");
					}
				}
			}
			
			if(e.getSource().equals(showdetil.getButton())){
				int index = friends.getSelectedRow();
				if(index ==-1){
					JOptionPane.showMessageDialog(null, "你还未选择数据行!");
				}else{
					addCard(bills.get(index).getId());
					showCard("002");
				}
				
			}
			
		}
    }
    
}
